package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.dao.GoodsCtrl;
import com.entity.Goods;;

/**
 * Servlet implementation class EditGoodsServlet
 */
@WebServlet("/EditGoodsServlet")
public class EditGoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditGoodsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Goods goods = new Goods();
		GoodsCtrl gc = new GoodsCtrl(); 
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
     
		int goods_id=Integer.parseInt(request.getParameter("goods_id"));
		
		String goods_name=request.getParameter("goods_name");
		int catalog_id = Integer.parseInt(request.getParameter("catalog_id"));
		String goods_type = request.getParameter("goods_type");
		int amount = Integer.parseInt(request.getParameter("amount"));
		int min_amount = Integer.parseInt(request.getParameter("min_amount"));
		int property_id=Integer.parseInt(request.getParameter("property_id"));
		String sn_id=request.getParameter("sn_id");
	
		goods.setGoods_id(goods_id);
		goods.setGoods_name(goods_name);
		goods.setCatalog_id(catalog_id);
        goods.setGood_type(goods_type);
        goods.setAmount(amount);
        goods.setMin_amount(min_amount);
        goods.setProperty_id(property_id);
        goods.setSn_id(sn_id);
        goods.setOrder_flag(true);

        System.out.println(catalog_id);
						
		int res =gc.updateGoods(goods);
		if(res>0){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index1.jsp");
			requestDispatcher.forward(request, response);
						
		}else{
			
			out.print("<script>alert('fail');window.history.back();</script>");
		}
	}

}
